package net.fiodor.log.channel {
	import net.fiodor.utils.StringUtil;
	
	import flash.display.AVM1Movie;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Loader;
	import flash.display.MorphShape;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.media.Video;
	import flash.text.StaticText;
	import flash.text.TextField;
	import flash.utils.getQualifiedClassName;		

	/**
	 * @author Tomasz Swierszcz, 
	 * email: tomasz.swierszcz@dreamlab.pl
	 */
	public class AbstractChannel {

		public var _maxDepth : Number;

		
		public function set maxDepth( value : Number ) : void { 
			_maxDepth = (_maxDepth > 255) ? 255 : value; 
		}

		public function get maxDepth() : Number { 
			return _maxDepth; 
		}

		public function AbstractChannel() : void {
			_maxDepth = 4;
		}

		public function toString() : String {
			
			return getQualifiedClassName(this).split(":")[getQualifiedClassName(this).split(":").length - 1];
			
		}

		public function analyzeObj( o : Object, depth : Number ) : String {
			var txt : String = "";
			var typeOf : String = typeof( o );
			if( typeOf == "string" ) {
				// STRING
				txt += o as String;
			} else if( typeOf == "boolean" || typeOf == "number" ) {
				// BOOLEAN / NUMBER
				txt += o as String;
			} else if( typeOf == "undefined" ) {
				// UNDEFINED
				txt += "( " + typeOf + " )";
			} else {
				// OBJECT
				var stringifyObj : Boolean = false;
				var analyze : Boolean = true;
				if( o is Array ) {
					// ARRAY
					typeOf = "array";
					stringifyObj = false;
				} else if( o is Date ) {
					// DATE
					typeOf = "date";
					analyze = false;
					stringifyObj = true;
					
				// DISPLAYOBJECT RELATED
				} else if( o is MovieClip ) {
					// MOVIECLIP
					typeOf = "movieclip";
					stringifyObj = true;
				} else if( o is Loader ) {
					// LOADER
					typeOf = "loader";
					stringifyObj = true;
				} else if( o is Sprite ) {
					// SPRITE
					typeOf = "sprite";
					stringifyObj = true;
				} else if( o is Stage ) {
					// STAGE
					typeOf = "stage";
					stringifyObj = true;
				} else if( o is DisplayObjectContainer ) {
					// DISPLAYOBJECTCONTAINER
					typeOf = "displayobjectcontainer";
					stringifyObj = true;
				} else if( o is SimpleButton ) {
					// SIMPLEBUTTON
					typeOf = "simplebutton";
					stringifyObj = true;
				} else if( o is TextField ) {
					// TEXTFIELD
					typeOf = "textfield";
					stringifyObj = true;
				} else if( o is AVM1Movie ) {
					// AVM1MOVIE
					typeOf = "avm1movie";
					stringifyObj = true;
				} else if( o is Bitmap ) {
					// BITMAP
					typeOf = "bitmap";
					stringifyObj = true;
				} else if( o is InteractiveObject ) {
					// INTERACTIVEOBJECT
					typeOf = "interactiveobject";
					stringifyObj = true;
				} else if( o is MorphShape ) {
					// MORPHSHAPE
					typeOf = "morphshape";
					stringifyObj = true;
				} else if( o is Shape ) {
					// SHAPE
					typeOf = "shape";
					stringifyObj = true;
				} else if( o is StaticText ) {
					// STATICTEXT
					typeOf = "statictext";
					stringifyObj = true;
				} else if( o is Video ) {
					// VIDEO
					typeOf = "video";
					stringifyObj = true;
				} else if( o is DisplayObject ) {
					// DISPLAYOBJECT
					typeOf = "displayobject";
					stringifyObj = true;
					
				// XML RELATED
				} else if( o is XMLList ) {
					// XMLLIST
					typeOf = "xmllist";
					analyze = false;
					stringifyObj = true;
				} else if( o is XML ) {
					// XML
					typeOf = "xml";
					analyze = false;
					stringifyObj = true;
				}
				txt += "( " + typeOf + " ) ";
				if( stringifyObj ) txt += o.toString();
				if( analyze && depth <= _maxDepth ) {
					var txtProps : String = "";
					for( var prop:String in o ) {
						txtProps += "\n" + StringUtil.multiply("\t", ( depth + 1 )) + prop + ":" + analyzeObj(o[prop], ( depth + 1 ));
					}
					if( txtProps.length > 0 ) txt += "{" + txtProps + "\n" + StringUtil.multiply("\t", depth) + "}";
				}
			}
			return txt;
		}
	}
}
